import numpy as np
import matplotlib.pyplot as plt
from math import ceil, floor, sqrt
from matplotlib import gridspec
from matplotlib import rcParams

rcParams['font.family'] = 'times new roman'
rcParams['font.size'] = 14
rcParams['font.weight'] = 'normal'

# topology
numToRs = 16
numHosts = 10
numLinks = 6
numSLinks = 2

# workloads
flowsDir = "case1/"
load = 0.6
time = 0.4
randomSeed = 1
workloadCDFs = ["VL2"]
workloadTypes = ["random"]

# scheduler
schedulers = ['Detour', 'Jelly', 'OSA', 'OvS']  # ,  "OvS"
interval = 0.4e-1  # Periodicity get the traffic matrix (Seconds)
adjust_delay = 10.0e-3  # Circuit switching time (Seconds) 10ms
sample_enable = True  #
sample_interval = 1.0e-4  # sample network throughput

iteration_a = 0
iteration_b = 1

inter = 5

x_osa = []
y_osa = []
x_ovs = []
y_ovs = []
x_detour = []
y_detour = []
x_jelly = []
y_jelly = []
counter = 0
for round_i in range(iteration_a, iteration_b):
    for workloadCDF in workloadCDFs:
        for workloadType in workloadTypes:
            for scheduler in schedulers:
                flowsFileName = "%s_%sToR_%sHosts_%sLinks_%sload_%stime_%s_%s_%s_sample.txt" \
                                % (scheduler, numToRs, numHosts, numLinks, load, time, workloadCDF, workloadType,
                                   round_i)

                with open(flowsFileName, 'r') as f:
                    for line in f.readlines():
                        split = line.split()
                        counter += 1
                        if len(split) == 4 and counter == inter:
                            counter = 0
                            if scheduler == "Detour":
                                x_detour.append(float(split[1]))
                                y_detour.append(float(split[-1]))
                            elif scheduler == "OvS":
                                x_ovs.append(float(split[1]))
                                y_ovs.append(float(split[-1]))
                            elif scheduler == "Fully":
                                x_osa.append(float(split[1]))
                                y_osa.append(float(split[-1]))
                            elif scheduler == "Jelly":
                                x_jelly.append(float(split[1]))
                                y_jelly.append(float(split[-1]))

lw = 1
ms = 6
fig = plt.figure(figsize=(5.8, 3.5))
ax = plt.axes()
l_osa, = plt.plot(x_osa, y_osa, 'r')
l_ovs, = plt.plot(x_ovs, y_ovs, color='b', lw=lw, ms=ms, zorder=3)
l_detour, = plt.plot(x_detour, y_detour, color='y', lw=lw, ms=ms, zorder=3)
l_jelly, = plt.plot(x_jelly, y_jelly, color='g', lw=lw, ms=ms, zorder=3)
# plt.xlim(0, 0.2)
plt.xlabel('Time (s)')
plt.ylabel('Network Throughput (bps)')
plt.subplots_adjust(bottom=0.15, top=0.92, left=0.15, right=0.95)
plt.legend((l_osa, l_ovs, l_detour, l_jelly), ('OSA', 'OVS', 'Detour', 'Jellyfish'), ncol=1, loc='lower right')


ax.yaxis.grid(zorder=0, ls='--')
# plt.savefig('0_Simulation_FCT.pdf')
plt.show()
